package net.caixiaomi.info.ui.discovery;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.LeagueMatchAdapter;
import net.caixiaomi.info.adapter.LeagueMatchBarAdapter;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.interfaces.ComonFragmentImpl;
import net.caixiaomi.info.model.LeagueDetailMatchListEntity;
import net.caixiaomi.info.model.LeagueFutureMatchInfoEntity;
import net.caixiaomi.info.model.LeagueMatchGroupEntity;
import net.caixiaomi.info.model.LeagueMatchTurnListEntity;
import net.caixiaomi.info.util.Constants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by mac on 2018/9/12.
 */

public class LeagueDetailMatchFragment extends BaseFragment implements ComonFragmentImpl,View.OnClickListener {

    private LeagueDetailMatchListEntity entity;

    private View mHeaderView;
    private TextView mBarTv;
    private ImageView mBarSelectView;
    private RecyclerView mBarListView;
    private LeagueMatchBarAdapter mBarAdapter;

    private int groupType;  //是否分组0:不分组,1分组 ,

    private int mCurrentIndex=0;

    @BindView(android.R.id.list)
    RecyclerView mListView;

    @BindView(R.id.no_data_tv)
    TextView mNoData;

    private LeagueMatchAdapter mAdapter;

    private boolean mBarSelect=false;

    @Override
    public void setType(int type) {

    }

    @Override
    public void setFilter(String filter) {

    }

    @Override
    protected int fragmentLayout() {
        return R.layout.league_shooter_layout;
    }

    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {
        mHeaderView=View.inflate(getActivity(),R.layout.league_detail_match_bar_layout,null);
        mBarListView=mHeaderView.findViewById(R.id.league_bar_list);
        mBarTv=mHeaderView.findViewById(R.id.match_bar_title);
        mBarListView.setLayoutManager(new GridLayoutManager(getActivity(),4));
        mBarAdapter=new LeagueMatchBarAdapter(R.layout.league_detail_bar_item_layout);
        mBarListView.setAdapter(mBarAdapter);
        mBarListView.setVisibility(View.GONE);
        mBarSelectView=mHeaderView.findViewById(R.id.match_bar_img);

        mBarAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mCurrentIndex=position;
                initData(entity);
                mBarSelect=false;
                mBarSelectView.setBackgroundResource(R.drawable.select_down);
                mBarListView.setVisibility(View.GONE);
            }
        });

        mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new LeagueMatchAdapter(null);
        mListView.setAdapter(mAdapter);

        mAdapter.addHeaderView(mHeaderView);

        LinearLayout lHeaderTitle=mHeaderView.findViewById(R.id.match_bar_title_group);
        mHeaderView.findViewById(R.id.league_match_left_img).setOnClickListener(this);
        mHeaderView.findViewById(R.id.league_match_right_img).setOnClickListener(this);
        lHeaderTitle.setOnClickListener(this);
    }

    public void initData(LeagueDetailMatchListEntity entity){
        this.entity=entity;

        List<MultiItemEntity>list0=new ArrayList<>();
        list0.addAll(entity.getMatchTurnGroupList());

        if(list0.size()==0){
            mNoData.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
            return;
        }
        mBarTv.setText(entity.getMatchTurnGroupList().get(mCurrentIndex).getTurnGroupName());
        mBarAdapter.setNewData(list0);

        groupType=entity.getMatchTurnGroupList().get(mCurrentIndex).getGroupType();
        List<LeagueMatchGroupEntity> data=entity.getMatchTurnGroupList().get(mCurrentIndex).getGroupDTOList();
        if(groupType==0){   //不分组
            if(data.size()>0) {
                List<LeagueFutureMatchInfoEntity> futureMatchDTOList = data.get(0).getFutureMatchDTOList();
                List<MultiItemEntity> list1 = new ArrayList<>();
                LeagueFutureMatchInfoEntity ee = new LeagueFutureMatchInfoEntity();
                ee.setType(Constants.ITEM_MATCH_LIVE1);
                list1.add(ee);
                list1.addAll(futureMatchDTOList);
                mAdapter.setNewData(list1);
            }
        }else{
            List<MultiItemEntity>list2=new ArrayList<>();
            for(int i=0;i<data.size();i++){
                List<LeagueFutureMatchInfoEntity> futureMatchDTOList=data.get(i).getFutureMatchDTOList();
                LeagueFutureMatchInfoEntity ee=new LeagueFutureMatchInfoEntity();
                ee.setType(Constants.ITEM_MATCH_LIVE2);
                list2.add(ee);
                list2.addAll(futureMatchDTOList);
                mAdapter.setNewData(list2);
            }
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.match_bar_title_group:
                mBarSelect=!mBarSelect;
                if(mBarSelect){
                    mBarSelectView.setBackgroundResource(R.drawable.select_up);
                    mBarListView.setVisibility(View.VISIBLE);
                }else{
                    mBarSelectView.setBackgroundResource(R.drawable.select_down);
                    mBarListView.setVisibility(View.GONE);
                }

                break;
            case R.id.league_match_left_img:
                mCurrentIndex--;
                if(mCurrentIndex<0){
                    mCurrentIndex=entity.getMatchTurnGroupList().size()-1;
                }
                initData(entity);
                mBarSelect=false;
                mBarSelectView.setBackgroundResource(R.drawable.select_down);
                mBarListView.setVisibility(View.GONE);
                break;
            case R.id.league_match_right_img:
                mCurrentIndex++;
                if(mCurrentIndex==entity.getMatchTurnGroupList().size()){
                    mCurrentIndex=0;
                }
                initData(entity);
                mBarSelect=false;
                mBarSelectView.setBackgroundResource(R.drawable.select_down);
                mBarListView.setVisibility(View.GONE);
                break;
        }
    }
}
